package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.IdWorker;
import com.hmall.order.utils.SnowflakeIdWorker;
import com.hmall.order.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    //private IdWorker idWorker = new IdWorker(10L,10L,0L);

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderLogisticsService orderLogisticsService;

    /**
     * 创建订单
     *
     * @param requestParams
     * @return
     */
    @Transactional
    @Override
    public Long createOrder(RequestParams requestParams) {
        //1. 根据雪花算法生成订单id
        //TableId(type = IdType.ASSIGN_ID)
        //long orderId = idWorker.nextId();
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(2);
        long orderSnowflakeId = snowflakeIdWorker.nextId();

        //2. 商品微服务提供FeignClient，实现根据id查询商品的接口
        //3. 根据itemId查询商品信息
        Item item = itemClient.getById(requestParams.getItemId());

        //4. 基于商品价格、购买数量计算商品总价：totalFee
        long totalFee = item.getPrice() * requestParams.getNum();
        Order order = new Order();
        order.setTotalFee(totalFee);
        order.setId(orderSnowflakeId);

        //5. 封装Order对象，初识status为未支付
        order.setPaymentType(requestParams.getPaymentType());
        order.setStatus(1);
        order.setUserId(UserHolder.getUser());

        //6. 将Order写入数据库tb_order表中
        orderMapper.insert(order);
        //this.save(order);

        //7. 将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail detail = new OrderDetail();
        detail.setOrderId(orderSnowflakeId);
        detail.setItemId(item.getId());
        detail.setNum(requestParams.getNum());
        detail.setName(item.getName());
        detail.setSpec(item.getSpec());
        detail.setPrice(item.getPrice());
        detail.setImage(item.getImage());
        orderDetailService.save(detail);

        //8. 根据addressId查询user-service服务，获取地址信息
        Long addressId = requestParams.getAddressId();
        Address address = userClient.findAddressById(addressId);

        //9. 将地址封装为OrderLogistics对象，写入tb_order_logistics表
        //新建OrderLogistics实例
        OrderLogistics orderLogistics = new OrderLogistics();
        //将地址转为OrderLogistics类型
        BeanUtils.copyProperties(address,orderLogistics);
        //设置缺少的字段
        orderLogistics.setOrderId(order.getId());
        //保存到物流表中
        orderLogisticsService.save(orderLogistics);

        //12. 调用item-service的减库存接口
        try {
            itemClient.updateStock(requestParams.getItemId(), requestParams.getNum());
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }

        return order.getId();
    }
}
